﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace B4Deal.Models
{
    public class Comment
    {
        private int maCM;

        public int MaCM
        {
            get { return maCM; }
            set { maCM = value; }
        }
        private int maNguoiPost;

        public int MaNguoiPost
        {
            get { return maNguoiPost; }
            set { maNguoiPost = value; }
        }
        private int maKM;

        public int MaKM
        {
            get { return maKM; }
            set { maKM = value; }
        }
        private string noiDung;

        public string NoiDung
        {
            get { return noiDung; }
            set { noiDung = value; }
        }
        private DateTime thoiGian;

        public DateTime ThoiGian
        {
            get { return thoiGian; }
            set { thoiGian = value; }
        }
        private int tinhTrang = 0; //Haven't pass by Admin

        public int TinhTrang
        {
            get { return tinhTrang; }
            set { tinhTrang = value; }
        }

        public Comment(string iNoiDung, DateTime iThoiGian, int iMaKH)
        {
            this.MaNguoiPost = iMaKH;
            this.NoiDung = iNoiDung;
            this.ThoiGian = iThoiGian;
        }

        public User QueryUserPostComment()
        {
            using (var db = new B4Deal_DataEntities())
            {
                var temp = db.TAIKHOANs.Where(u => u.MaTK == this.MaNguoiPost).ToList()[0];
                return new User(temp.MaTK, temp.HoTen, temp.Email, temp.Password, Convert.ToBoolean(temp.GioiTinh), Convert.ToDateTime(temp.NgaySinh), Convert.ToInt32(temp.TinhTrang));
            }
        }

        public bool SendComment()
        {
            using (var localDB = new B4Deal_DataEntities())
            {
                COMMENT c = new COMMENT();
                c.NoiDung = NoiDung;
                c.NguoiPost = MaNguoiPost;
                c.ThoiGian = ThoiGian;
                c.TinhTrang = TinhTrang;
                c.KhuyenMai = MaKM;
                try
                {
                    localDB.AddToCOMMENTs(c);
                    localDB.SaveChanges();
                }
                catch (ArgumentException ae)
                {
                    throw ae;                    
                }
                catch (Exception e)
                {
                    throw new ArgumentException("Có lỗi xảy ra! Xin vui lòng kiểm tra và thử lại. " +
                        "Nếu vẫn còn lỗi xin liên hệ với admin.");                    
                }
                return true;
            }            
        }
    }
}